//
//  QDUserInfoModel.swift
//  QDMetroClient
//
//  用户信息以及用户状态
//
//  Created by YueWen on 2019/3/4.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit
import HandyJSON


/// 用户状态
final class QDUserStatus: NSObject {
    
    /// 用于记录用户是否登陆
    private static let loginKey = "NSUserDefaults_User_Login_Key"
    
    /// 是否已经登录
    @objc static var isLogin: Bool {
        set {
            UserDefaults.standard.set(newValue, forKey: loginKey)
            //发送通知
            if newValue {
                NotificationCenter.default.post(name: QDLoginCompleteNotificationName, object: nil)
            }else {
                NotificationCenter.default.post(name: QDLogoutCompleteNotificationName, object: nil)
            }
            
        }get {
            print("\n\n\nuserdefaults = \(UserDefaults.standard.bool(forKey: loginKey))\n\n\n")
            return UserDefaults.standard.bool(forKey: loginKey)
        }
    }
    
    /// 是否是NFC登陆
    @objc static var isNFC: Bool {
        set {
            UserDefaults.standard.set(newValue, forKey: "isNFC")
        }get {
            print("\n\n\nNFC = \(UserDefaults.standard.bool(forKey: "isNFC"))\n\n\n")
            return UserDefaults.standard.bool(forKey: "isNFC")
        }
    }
}


/// 给Objc暴露的接口
final class QDUserInfoModelObjc: NSObject {
    
    @objc static var token: String {
        get {
            return QDUserInfoModel.share.token
        }
    }
    
    @objc static var phone: String {
        get {
            return QDUserInfoModel.share.phone
        }
    }
}



/// 用户的模型
final class QDUserInfoModel: NSObject,HandyJSON {
    
    /// 票
    class NFCTicket: HandyJSON {
        
        var mode = ""
        var qr = ""
        var ticket = ""
        
        required init() {}
    }
    
    /// 单例对象
    static let share = QDUserInfoModel()
    
    /// 用户id
    var id = ""
    /// 用户昵称
    var nickName = ""
    /// 用户手机号
    var phone = ""
    /// 用户令牌
    var token = ""
    /// 用户头像
    var image = ""
    /// 积分数
    var creditSum = ""
    /// 用户私钥
    var privateKey = ""
    /// 用户公钥
    var publicKey = ""
    /// 有效时间
    var validityTime = ""
    /// NFC
    var ticket: NFCTicket? {
        willSet {
            UserDefaults.standard.set(newValue == nil ? false : true, forKey: "isNFC")
        }
    }
    /// 当前绑定类型
    var currentChannel = "" {
        willSet {
            UserDefaults.standard.set(newValue, forKey: "currentChannel")
        }
    }

    /// 是不是NFC
    func isNFC() -> Bool {
        return !(ticket == nil)
    }

    ///
    func mapping(mapper: HelpingMapper) {
       mapper <<<
        self.id <-- "Id"
        
      mapper <<<
        self.phone <-- "Phone"
    }
    
    
    /// 保存到本地
    func saveToNativePlace(){
        
        let userDefaults = UserDefaults.standard
        
        //进行存储
        userDefaults.set(id, forKey: "id")
        userDefaults.set(nickName, forKey: "nickName")
        userDefaults.set(phone, forKey: "phone")
        userDefaults.set(token, forKey: "token")
        userDefaults.set(image, forKey: "image")
        userDefaults.set(privateKey, forKey: "privateKey")
        userDefaults.set(publicKey, forKey: "publicKey")
        userDefaults.set(validityTime, forKey: "validityTime")
        userDefaults.set(creditSum, forKey: "creditSum")
        userDefaults.set(currentChannel, forKey: "currentChannel")
        
        if ticket == nil { //不是NFC
            QDUserStatus.isNFC = false
        }else { // 是NFC
            QDUserStatus.isNFC = true
        }
    }
    
    
    /// 从本地读取,请从 QDUserInfoModel.share 中读取
    @objc static func readFromNativePlace(){
        /// 获得单例对象
        let (shareIntance,userDefaults) = (QDUserInfoModel.share,UserDefaults.standard)
        
        shareIntance.id = userDefaults.value(forKey: "id") as? String ?? ""
        shareIntance.nickName = userDefaults.value(forKey: "nickName") as? String ?? ""
        shareIntance.phone = userDefaults.value(forKey: "phone") as? String ?? ""
        shareIntance.token = userDefaults.value(forKey: "token") as? String ?? ""
        shareIntance.image = userDefaults.value(forKey: "image") as? String ?? ""
        shareIntance.privateKey = userDefaults.value(forKey: "privateKey") as? String ?? ""
        shareIntance.publicKey = userDefaults.value(forKey: "publicKey") as? String ?? ""
        shareIntance.validityTime = userDefaults.value(forKey: "validityTime") as? String ?? ""
        shareIntance.currentChannel = userDefaults.value(forKey: "currentChannel") as? String ?? ""
        shareIntance.creditSum = userDefaults.value(forKey: "creditSum") as? String ?? "0"
    }
}
